package com.sti.platform.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.aisino.core.common.JsonPost;
import com.aisino.core.controller.BaseController;
import com.sti.platform.model.User;
import com.sti.platform.service.UserService;

@Controller()
@RequestMapping("user")
public class UserController extends BaseController{
	
	@Autowired
	private UserService userService;
	
	/**
	 * 查询user列表
	 * @param request
	 * @return
	 */
	@RequestMapping("list")
	public String userPage(HttpServletRequest request,Integer page,Integer row){
		String condition = request.getParameter("condition");
		List<User> list = userService.queryUsersByCondition(condition,page,row);
		
		request.setAttribute("list", list);
		return "user/user";
	}
	
	/**
	 * 
	 * 跳转到添加用户页面
	 * @param request
	 * @return
	 */
	@RequestMapping("addHtml")
	public String queryAddInfoPage(HttpServletRequest request){
		Map<String,Object> map = userService.queryAddInfo();
		request.setAttribute("map", map);
		return "user/add_user";
	}
	
	/**
	 * 添加用户
	 * @param user
	 * @param roles
	 * @return
	 */
	@RequestMapping("addUser")
	public @ResponseBody Map addUserPage(final User user,final String role){
		final List<String> roles = Arrays.asList(role.split(","));
		return super.jsonPost(new JsonPost(){
			@SuppressWarnings("unchecked")
			public void doInstancePost(Map map){
				 userService.addUser(user,roles);
			}
		});
	}
	/**
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("editorHtml")
	public String editorPage(HttpServletRequest request){
		String userId = request.getParameter("userId");
		Map<String,Object> map = userService.queryAddInfo();
		Map<String,Object> userInfo = userService.queryUserInfo(userId);
		
		map.put("userInfo", userInfo);
		request.setAttribute("map", map);
		return "user/editor_user";
	}
	
	/**
	 * 修改用户信息
	 * @param user
	 * @return
	 */
	@RequestMapping("editorUser") 
	public @ResponseBody Map editorUserPage(final User user,final String role){
		final List<String> roles = Arrays.asList(role.split(","));
		return super.jsonPost(new JsonPost(){
			public void doInstancePost(Map map){
				userService.updateUser(user,roles);
			}
		});
	}
	
	/**
	 * 删除用户信息
	 * @param user
	 * @return
	 */
	@RequestMapping("delectUser") 
	public @ResponseBody Map delectUser(final String userId){
		return super.jsonPost(new JsonPost(){
			public void doInstancePost(Map map){
				userService.delectUser(userId);
			}
		});
	}
	
	/**
	 * 批量删除用户信息
	 * @param user
	 * @return
	 */
	@RequestMapping("delectUsers") 
	public @ResponseBody Map delectUsers(final String userIds){
		return super.jsonPost(new JsonPost(){
			public void doInstancePost(Map map){
				userService.delectUsers(userIds);
			}
		});
	}
	
	/**
	 * 导出
	 * @param response
	 */
	@RequestMapping("export") 
	@ResponseBody
	public void exportUser(HttpServletResponse response){
		userService.eportUser(response);
	}
	
	/**
	 * 导入
	 * @param file
	 */
	@RequestMapping("import") 
	@ResponseBody
	public void importUser(MultipartFile file){
		
	}
	
}
